package com.dgshanger.sanhxiaofeisc.ui.nearby

import android.app.Activity
import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.CategoryAdapter
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.NearbyItemEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CategoryContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.CategoryPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.activity.ShopDetailsActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import kotlinx.android.synthetic.main.activity_category.*


/**
 * CategoryActivity
 * 沉迷学习不能自拔
 * Describe：分类查询
 * Created by 雷小星🍀 on 2018/4/19 11:47.
 */
class CategoryActivity : BaseMvpLcecActivity<SwipeRefreshLayout,List<NearbyItemEntity>,CategoryContract.Model,CategoryContract.View,CategoryPresenter>(),CategoryContract.View, View.OnClickListener {

    private var currentPage : Int=1
    private lateinit var mCategory_id : String
    private lateinit var mLocation : Array<String>
    private lateinit var mAdapter : CategoryAdapter
    private lateinit var mCategory_name : String

    override fun getActivityLayoutId(): Int = R.layout.activity_category

    override fun createPresenter(): CategoryPresenter = CategoryPresenter()

    override fun initData() {
        super.initData()
        mCategory_id=intent.getStringExtra("category_id")
        mCategory_name=intent.getStringExtra("category_name")
        mLocation= Constants.getLocation()
        initView()
        loadData(true)
    }

    private fun initView() {
        ll_distance.setOnClickListener(this)
        ll_comment.setOnClickListener(this)
        ll_new.setOnClickListener(this)
        category_title.text=mCategory_name
        recyclerView.layoutManager=LinearLayoutManager(mContext)
        mAdapter= CategoryAdapter(R.layout.item_nearby_list,ArrayList())
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
        recyclerView.adapter=mAdapter
        mAdapter.setOnLoadMoreListener {
            loadData(false)
        }
    }

    override fun initEvent() {
        //刷新
        contentView.setOnRefreshListener {
            if (!presenter.isLoading!!) {
                currentPage=1
                loadData(true)
            }
        }

        recyclerView.addOnItemTouchListener(object : OnItemClickListener(){

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val intent = Intent(mContext, ShopDetailsActivity::class.java)
                intent.putExtra("shop_id",mAdapter.data[position].shop_id)
                intent.putExtra("comment_score",mAdapter.data[position].comment_score)
                startActivity(intent)
            }

        })

        //键盘确认键的监听
        et_search.setOnEditorActionListener(object : TextView.OnEditorActionListener{
            override fun onEditorAction(v: TextView?, actionId: Int, event : KeyEvent?): Boolean {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    val searchContent = et_search.text.toString().trim()
                    if (!TextUtils.isEmpty(searchContent)){
                        mShop_name=searchContent
                        setDelUI()
                        currentPage=1
                        mSort_distance=null
                        mSort_new=null
                        mSort_comment=null
                        showLoadingDialog(getString(R.string.tv_searching))
                        loadData(true)
                        KeyboardUtils.hideSoftInput(mContext as Activity)
                    }
                }
                return false
            }
        })
    }

    override fun loadData(refresh: Boolean) {

        presenter.loadData(refresh,mLocation[2],mSort_distance,mLocation[1],
                mLocation[0],currentPage.toString(),mSort_new,mSort_comment,mShop_name,mCategory_id)
    }

    override fun onClick(view: View?) {
        when(view!!.id){
            R.id.ll_distance->{//距离
                changeUI(0)
            }
            R.id.ll_comment->{//评价
                changeUI(1)
            }
            R.id.ll_new->{//最新入驻
                changeUI(2)
            }
        }
    }

    private var isAsc : Boolean =true //是否升序
    private var typeFlag : Int= -1 // 表示是否点击同一个
    private var mSort_distance : String ?= null
    private var mSort_new : String ?= null
    private var mSort_comment : String ?= null
    private var mShop_name : String ? =null
    private fun changeUI(type: Int) {
        if (typeFlag!=type){
            isAsc=true
            typeFlag=type
        }
        setDelUI()
        currentPage=1
        when(type){
            0->{//距离
                if (isAsc){//升序
                    iv_distance_up.setImageResource(R.drawable.xiaoshang_)
                    mSort_distance="asc"
                    mSort_new=null
                    mSort_comment=null
                }else{
                    iv_distance_dw.setImageResource(R.drawable.xiaoxia_)
                    mSort_distance="desc"
                    mSort_new=null
                    mSort_comment=null
                }
                isAsc=!isAsc
            }
            1->{//评价
                if (isAsc){
                    iv_comment_up.setImageResource(R.drawable.xiaoshang_)
                    mSort_distance=null
                    mSort_new=null
                    mSort_comment="asc"
                }else{
                    iv_comment_dw.setImageResource(R.drawable.xiaoxia_)
                    mSort_distance=null
                    mSort_new=null
                    mSort_comment="desc"
                }
                isAsc=!isAsc
            }
            2->{//最新入驻
                if (isAsc){
                    iv_new_up.setImageResource(R.drawable.xiaoshang_)
                    mSort_distance=null
                    mSort_new="asc"
                    mSort_comment=null
                }else{
                    iv_new_dw.setImageResource(R.drawable.xiaoxia_)
                    mSort_distance=null
                    mSort_new="desc"
                    mSort_comment=null

                }
                isAsc=!isAsc
            }
        }
        showLoadingDialog()
        loadData(true)
    }

    private fun setDelUI() {
        iv_distance_up.setImageResource(R.drawable.xiaosan_)
        iv_distance_dw.setImageResource(R.drawable.huishan_)
        iv_comment_up.setImageResource(R.drawable.xiaosan_)
        iv_comment_dw.setImageResource(R.drawable.huishan_)
        iv_new_up.setImageResource(R.drawable.xiaosan_)
        iv_new_dw.setImageResource(R.drawable.huishan_)
    }

    override fun setData(data: List<NearbyItemEntity>?) {
        showContent()
        currentPage++
        contentView.isRefreshing=false
        mAdapter.setNewData(data)
    }

    override fun addData(data: MutableList<NearbyItemEntity>?) {
        currentPage++
        if (data!=null&&!data.isEmpty()){
            mAdapter.loadMoreComplete()
            mAdapter.addData(data)
        }else{
            mAdapter.loadMoreEnd()
        }
    }

    override fun onDataFailure(err: String?) {
        mAdapter.loadMoreFail()
    }
}